import sys
from PySide6.QtWidgets import (
    QApplication,
    QWidget,
    QVBoxLayout,
    QHBoxLayout,
    QPushButton,
    QComboBox,
)
import pyqtgraph as pg
import numpy as np


class CoordinateVisualizer(QWidget):
    """坐标可视化器"""

    def __init__(self):
        super().__init__()
        # 初始化数据
        self.data = [
            {"name": "Point1", "value": [1, 2]},
            {"name": "Point2", "value": [2, 3]},
            {"name": "Point3", "value": [3, 4]},
        ]
        # 存储当前原点位置
        self.origin_position = "left-bottom"  # 默认左下角
        self.init_ui()
        self.update_plot()

    def init_ui(self):
        """初始化用户界面"""
        layout = QVBoxLayout(self)

        # 控制面板
        control_panel = QWidget()
        control_layout = QHBoxLayout(control_panel)

        # 原点位置选择器
        self.origin_selector = QComboBox()
        self.origin_selector.addItems(["左下角", "左上角", "右下角", "右上角", "中心"])
        self.origin_selector.currentTextChanged.connect(self.on_origin_changed)
        control_layout.addWidget(self.origin_selector)

        layout.addWidget(control_panel)

        # 绘图窗口
        self.plot_widget = pg.PlotWidget()
        self.plot_widget.showGrid(x=True, y=True)
        layout.addWidget(self.plot_widget)

        self.setWindowTitle("坐标系可视化")
        self.resize(600, 400)

    def get_origin_offset(self):
        """
        计算原点偏移量

        Returns:
            tuple: (x偏移, y偏移)
        """
        # 获取数据范围
        x_values = [p["value"][0] for p in self.data]
        y_values = [p["value"][1] for p in self.data]
        x_min, x_max = min(x_values), max(x_values)
        y_min, y_max = min(y_values), max(y_values)

        # 根据选择的原点位置计算偏移
        origin_map = {
            "左下角": (0, 0),
            "左上角": (0, -y_max),
            "右下角": (-x_max, 0),
            "右上角": (-x_max, -y_max),
            "中心": (-x_max / 2, -y_max / 2),
        }

        return origin_map.get(self.origin_selector.currentText(), (0, 0))

    def update_plot(self):
        """更新图形显示"""
        self.plot_widget.clear()

        # 提取数据
        x = np.array([item["value"][0] for item in self.data])
        y = np.array([item["value"][1] for item in self.data])

        # 应用原点偏移
        x_offset, y_offset = self.get_origin_offset()
        x = x + x_offset
        y = y + y_offset

        # 创建散点图
        scatter = pg.ScatterPlotItem(
            x=x,
            y=y,
            symbol="o",
            size=10,
            pen=pg.mkPen(None),
            brush=pg.mkBrush(0, 0, 255, 200),
            hoverable=True,
            hoverBrush=pg.mkBrush(255, 0, 0, 200),
            hoverSize=15,
        )

        self.plot_widget.addItem(scatter)

        # 添加坐标轴
        self.plot_widget.showGrid(x=True, y=True)

    def on_origin_changed(self, position):
        """
        处理原点位置变化

        Args:
            position: 新的原点位置
        """
        self.origin_position = position
        self.update_plot()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = CoordinateVisualizer()
    window.show()
    sys.exit(app.exec())